import time


from selenium.webdriver.ie.webdriver import WebDriver
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from producttest.caw.logger import Logger


# 定义一个函数，来获取不同浏览器的驱动
def getDriver(driverStr):
    if driverStr == 'safari':
        driver = webdriver.Safari()
    elif driverStr == 'chrome':
        driver = webdriver.Chrome()
    elif driverStr == 'Firefox':
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Edge()
    return driver


# 定义一个类，封装了不同的操作：关闭浏览器，定位元素 选择下拉框...
class BasePage:
    def __init__(self, driver: webdriver):
        self.driver = driver

    # 打开某网页
    def open(self, url):
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()
        # 隐式等待
        self.driver.implicitly_wait(5)

    # 退出浏览器
    def quit(self):
        self.driver.quit()

    # 关闭当前网页
    def close(self):
        self.driver.close()

    # 定位元素

    def locate_element(self, target):
        try:
            # 处理target字符串
            list1 = target.split('=')
            locator = (list1[0].strip(), list1[1].strip())

            # 显示等待
            element = WebDriverWait(self.driver, 5, 0.5).until(
                expected_conditions.presence_of_element_located(locator))
            logger = Logger()
            logger.error(f"-----定位成功------")
            return element
        except Exception as e:
            logger = Logger()
            logger.error(f"-----定位失败：{e}------")

    # 发送信息
    def send_information(self, target, text):
        element = self.locate_element(target)
        if element is not None:
            print('--------')
            try:
                # 清空输入框再发送（可选操作，根据实际需求调整）
                element.clear()
                element.send_keys(text)
                logger = Logger()
                logger.error(f"-----向元素{target}发送文本成功：{text}------")
            except Exception as e:
                logger = Logger()
                logger.error(f"-----向元素{target}发送文本失败：{e}------")


    # 点击信息
    def click_element(self, target):
        try:
            element = self.locate_element(target)
            element.click()
            logger = Logger()
            logger.error(f"-----元素{target}点击成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----元素{target}点击失败：{e}------")


    # 跳转到iframe
    def switch_to_iframe(self, target):
        # 切换到指定iframe
        try:
            iframe_element = self.locate_element(target)
            self.driver.switch_to.frame(iframe_element)
            logger = Logger()
            logger.error(f"-----切换到iframe {target} 成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----切换到iframe {target} 失败: {e}------")

    # 返回上一层
    def switch_to_parent_frame(self):
        # 返回到上一级iframe
        try:
            self.driver.switch_to.parent_frame()
            logger = Logger()
            logger.error(f"-----返回上一层iframe成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----返回上一层iframe失败: {e}------")

    # 返回最外层
    def switch_to_default_content(self):
        # 返回到最外层页面（退出所有iframe）"""
        try:
            self.driver.switch_to.default_content()
            logger = Logger()
            logger.error(f"-----返回到最外层页面成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----返回到最外层页面失败: {e}------")

    # 根据下标进行选择下拉框
    def select_by_index(self, target, index):
        # 通过下标选择下拉框选项

        try:
            select_element=self.locate_element(target)
            select=Select(select_element)
            select.select_by_index(index)
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过下标 {index} 选择成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过下标选择失败: {e}------")

    # 根据文本选择下拉框
    def select_by_text(self,target,text):
        try:
            select_element = self.locate_element(target)
            select=Select(select_element)
            select.select_by_visible_text(text)
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过文本 {text} 选择成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过文本选择失败: {e}------")
    # 根据value进行选择下拉框
    def select_by_value(self, target, value):

        try:
            """通过value属性选择下拉框选项"""
            select_element = self.locate_element(target)
            select = Select(select_element)
            select.select_by_value(value)
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过value {value} 选择成功------")
        except Exception as e:
            logger = Logger()
            logger.error(f"-----下拉框 {target} 通过value选择失败: {e}------")

if __name__ == '__main__':
    # 创建基础类对象
    bp=BasePage(getDriver(''))
    bp.open("http://www.sogou.com")
    # 定位元素
    bp.locate_element("id = query")
    time.sleep(2)

    # 发送
    bp.send_information("id = query", "selenium")
    bp.click_element("id = stb")
    time.sleep(10)